// app/src/main/java/com/example/myapplication/ProfileFragment.java
package com.example.myapplication;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

public class ProfileFragment extends Fragment {

    private TextView tvUsername;
    private Button btnMyArticles, btnMyOrders, btnMyCart;
    private View btnSettings;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_profile, container, false);

        tvUsername = view.findViewById(R.id.tv_profile_username);
        btnMyArticles = view.findViewById(R.id.btn_my_articles);
        btnMyOrders = view.findViewById(R.id.btn_my_orders);
        btnMyCart = view.findViewById(R.id.btn_my_cart);
        btnSettings = view.findViewById(R.id.btnsettings);

        // 获取当前登录用户
        SharedPreferences prefs = requireActivity().getSharedPreferences("user_prefs", requireActivity().MODE_PRIVATE);
        String currentUser = prefs.getString("current_username", "未登录");
        tvUsername.setText(currentUser);

        // 我的文章点击事件
        btnMyArticles.setOnClickListener(v -> {
            // 复用 NewsFragment 界面，可根据需求添加筛选条件
            getParentFragmentManager().beginTransaction()
                    .replace(R.id.fragment_container, new NewsFragment())
                    .commit();
        });

        // 我的订单点击事件
        btnMyOrders.setOnClickListener(v -> {
            // 复用 ShopFragment 界面，可根据需求添加筛选条件
            getParentFragmentManager().beginTransaction()
                    .replace(R.id.fragment_container, new ShopFragment())
                    .commit();
        });

        // 购物车点击事件
        btnMyCart.setOnClickListener(v -> {
            Intent intent = new Intent(requireActivity(), CartActivity.class);
            startActivity(intent);
        });

        // 新增设置按钮点击事件
        btnSettings.setOnClickListener(v -> {
                    startActivity(new Intent(requireActivity(), SettingsActivity.class));

        });
        return view;
    }
}